package com.hrm.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hrm.dao.PersonDao;
import com.hrm.dao.UserMapper;
import com.hrm.entity.Message;
import com.hrm.entity.Person;
import com.hrm.entity.User;

@Controller
@RequestMapping("/rest/user")
public class RestAction {
	@Autowired
	private UserMapper userMapper;
	
	
	public UserMapper getUserMapper() {
		return userMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}


	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<User> getAll() {
		return userMapper.selectAllUser();
	}

	@RequestMapping(value = "/{id}", method = {RequestMethod.GET})
	@ResponseBody
	public User getUser(@PathVariable("id") Long id) {
		return userMapper.selectUserById(id);
	}

	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public User savePerson(@RequestBody User user) {
		int id = userMapper.insertUser(user);//������ݺ��ID��ͨ��user.getId()��ȡ
		return user;
	}
	
	@RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
	@ResponseBody
	public Message deleteUser(@PathVariable("id") Long id) {
		userMapper.deleteUserById(id);
		return new Message("Success","Delete person :"+id+" Success!",new Date());
	}
	
	@RequestMapping(value = "/{id}",method = RequestMethod.PUT)
	@ResponseBody
	public User updateUser(@PathVariable("id") Long id,@RequestBody User user) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("name", user.getUserName());
		userMapper.updateUserByMap(map);
		return userMapper.selectUserById(id);
		
	}
	
}